<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="loginname">
      <el-input v-model="dataForm.loginname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="pwd">
      <el-input v-model="dataForm.pwd" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="address">
      <el-input v-model="dataForm.address" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sex">
      <el-input v-model="dataForm.sex" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="remark">
      <el-input v-model="dataForm.remark" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deptid">
      <el-input v-model="dataForm.deptid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="hiredate">
      <el-input v-model="dataForm.hiredate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="上级领导id" prop="mgr">
      <el-input v-model="dataForm.mgr" placeholder="上级领导id"></el-input>
    </el-form-item>
    <el-form-item label="是否可用，0不可用，1可用" prop="available">
      <el-input v-model="dataForm.available" placeholder="是否可用，0不可用，1可用"></el-input>
    </el-form-item>
    <el-form-item label="排序码" prop="ordernum">
      <el-input v-model="dataForm.ordernum" placeholder="排序码"></el-input>
    </el-form-item>
    <el-form-item label="用户类型[0超级管理员，1管理员，2普通用户]" prop="type">
      <el-input v-model="dataForm.type" placeholder="用户类型[0超级管理员，1管理员，2普通用户]"></el-input>
    </el-form-item>
    <el-form-item label="用户头像地址" prop="imgpath">
      <el-input v-model="dataForm.imgpath" placeholder="用户头像地址"></el-input>
    </el-form-item>
    <el-form-item label="盐" prop="salt">
      <el-input v-model="dataForm.salt" placeholder="盐"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          loginname: '',
          pwd: '',
          address: '',
          sex: '',
          remark: '',
          deptid: '',
          hiredate: '',
          mgr: '',
          available: '',
          ordernum: '',
          type: '',
          imgpath: '',
          salt: ''
        },
        dataRule: {
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          loginname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          deptid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          hiredate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          mgr: [
            { required: true, message: '上级领导id不能为空', trigger: 'blur' }
          ],
          available: [
            { required: true, message: '是否可用，0不可用，1可用不能为空', trigger: 'blur' }
          ],
          ordernum: [
            { required: true, message: '排序码不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '用户类型[0超级管理员，1管理员，2普通用户]不能为空', trigger: 'blur' }
          ],
          imgpath: [
            { required: true, message: '用户头像地址不能为空', trigger: 'blur' }
          ],
          salt: [
            { required: true, message: '盐不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.user.name
                this.dataForm.loginname = data.user.loginname
                this.dataForm.pwd = data.user.pwd
                this.dataForm.address = data.user.address
                this.dataForm.sex = data.user.sex
                this.dataForm.remark = data.user.remark
                this.dataForm.deptid = data.user.deptid
                this.dataForm.hiredate = data.user.hiredate
                this.dataForm.mgr = data.user.mgr
                this.dataForm.available = data.user.available
                this.dataForm.ordernum = data.user.ordernum
                this.dataForm.type = data.user.type
                this.dataForm.imgpath = data.user.imgpath
                this.dataForm.salt = data.user.salt
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'loginname': this.dataForm.loginname,
                'pwd': this.dataForm.pwd,
                'address': this.dataForm.address,
                'sex': this.dataForm.sex,
                'remark': this.dataForm.remark,
                'deptid': this.dataForm.deptid,
                'hiredate': this.dataForm.hiredate,
                'mgr': this.dataForm.mgr,
                'available': this.dataForm.available,
                'ordernum': this.dataForm.ordernum,
                'type': this.dataForm.type,
                'imgpath': this.dataForm.imgpath,
                'salt': this.dataForm.salt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
